import { defineStore } from "pinia"
import { addMember, getMember, delMember, editMember,queryMember } from "@/api/teacherList/index"
//引入消息提示
import { ElMessage } from 'element-plus'

export const setTeacher = defineStore("teacher", {
    state: () => ({

    }),
    actions: {
        async memberInfo(currentPage: number, pageSize: number) {
            let res = await getMember(currentPage, pageSize)
            if (res.code == 200) {
                return Promise.resolve(res.data.teacher)
            } else {
                return Promise.reject("获取信息失败")
            }
        },
        async addMember(data: any) {
            let needData: any = {}
            needData.teacherName = data.name
            needData.post = data.post
            let res = await addMember(needData)

            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        },
        async editMeber(id:number,data:any){
            let Data:any = {}
            Data.id = id
            Data.teacherName = data.name
            Data.performance = data.post
            let res = await editMember(Data)
            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        },
        async delMember(id:number){
          
            let res = await delMember(id)
            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        },
        async queryMember(id:number){
            let res = await queryMember(id)
            if (res.code == 200) {
                return Promise.resolve(res.data.teacher)
            } else {
                return Promise.reject("获取信息失败")
            }
        }   
    },
    getters: {}
})